package com.example.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.Constants;
import com.example.common.Result;
import com.example.entry.OrderForm;
import com.example.entry.Rider;
import com.example.entry.RiderEvaluate;
import com.example.exception.ServiceException;
import com.example.service.IOrderFormService;
import com.example.service.IRiderEvaluateService;
import com.example.service.IRiderService;
import org.apache.commons.collections4.Get;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;


/*
 * 骑手前台控制器
 * */
@RestController
@RequestMapping("/web/rider")
public class RiderWebController {

    @Resource
    private IRiderService riderService;

    @Resource
    private IRiderEvaluateService riderEvaluateService;

    @Resource
    private IOrderFormService orderFormService;

    /*
     * 注册骑手
     * */
    @PostMapping("/register-rider")
    public Result registerRider(@RequestBody Rider rider){
        riderService.save(rider);
        return Result.success("注册成功");
    }

    @PostMapping("/update")
    public Result updateRiderInfo(@RequestBody Rider rider){
        System.out.println(rider.getId());
        boolean b = riderService.updateById(rider);
        return Result.success("修改成功", b);
    }

    /*
     * 根据用户id查询骑手信息
     * */
    @GetMapping("/findr-rider/id={userId}")
    public Result getRiderInfoById(@PathVariable Integer userId){
        try {
            QueryWrapper<Rider> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            Rider rider = riderService.getOne(wrapper);
            return Result.success("查询成功", rider);
        } catch (Exception e){
            throw  new ServiceException(Constants.CODE_500,"没有查询到骑手信息，请联系管理人员");
        }
    }

    /*
    * 骑手评价
    * */
    @PostMapping("/evaluate")
    public Result riderEvaluate(@RequestBody RiderEvaluate riderEvaluate){
        // 修改订单状态
        OrderForm orderForm = orderFormService.getById(riderEvaluate.getOrderId());
        orderForm.setIsEvaluate(1);
        orderFormService.updateById(orderForm);

        riderEvaluateService.save(riderEvaluate);
        return Result.success("保存成功");
    }
    /*
    * 查询骑手评价
    * */
    @GetMapping("/evaluate/id={id}")
    public Result getRiderEvaluate(@PathVariable Integer id){
        try {
            QueryWrapper<RiderEvaluate> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", id);
            RiderEvaluate serviceOne = riderEvaluateService.getOne(wrapper);
            return Result.success("success", serviceOne);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_600, "查询失败");
        }

    }
}
